package com.tianzmp.ttq.admin.controller;

import com.tianzmp.ttq.admin.common.BaseResult;
import com.tianzmp.ttq.admin.common.Result;
import com.tianzmp.ttq.admin.controller.reqDto.RoleAuthorMenuDTO;
import com.tianzmp.ttq.admin.controller.resDto.MenuTree;
import com.tianzmp.ttq.admin.controller.resDto.ResponseUserListDto;
import com.tianzmp.ttq.admin.entity.PageInfo;
import com.tianzmp.ttq.admin.entity.Role;
import com.tianzmp.ttq.admin.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author ttq
 * @since 2022-07-30
 */
@RestController
@RequestMapping("/admin/role")
public class RoleController {

    @Resource
    private IRoleService roleService;


    /**
     * <pre>
     *     this is a user list
     * </pre>
     * @return
     */
    @ApiOperation(value = "不分页查询用户",notes = "不分页查询所有用户",httpMethod = "GET")
    @GetMapping("/list")
    @ApiResponses(value = {@ApiResponse(code = 200,message = BaseResult.SUCCESS_MSG,response = ResponseUserListDto.class)})
    public BaseResult<List<Role>> list(){
        return Result.ok(roleService.list());
    }

    /**
     * <pre>
     *     user list is a pageInfo
     * </pre>
     * @param current  当前第几页
     * @param size   当前页条数
     * @return
     */
    @ApiOperation(value = "分页查询角色",notes = "分页查询角色信息",httpMethod = "POST")
    @PostMapping(value = "/pageList")
    public BaseResult<PageInfo<List<Role>>> pageList(
            @RequestParam(required = false,defaultValue = "1",value = "current") Integer current,
            @RequestParam(required = false,defaultValue = "10",value = "size") Integer size
    ){
        return Result.ok(roleService.pageList(current, size));
    }


    /**
     * <pre>
     *
     * </pre>
     * @param roleId role-id
     * @return
     */
    @ApiOperation(value = "查询角色详情",notes = "根据roleiId查询角色详情",httpMethod = "GET")
    @GetMapping(value = "/getById/{roleId}")
    public BaseResult<Role> getById(@PathVariable(value = "roleId",required = true)Long roleId){
        return Result.ok(roleService.getById(roleId));
    }

    @ApiOperation(value = "更新角色",notes = "根据roleId更新角色",httpMethod = "POST")
    @PostMapping(value = "/update")
    public BaseResult<Boolean> update(@Valid @RequestBody Role dto){
        return Result.ok(roleService.update(dto));
    }


    @ApiOperation(value = "新增角色",notes = "新增角色",httpMethod = "POST")
    @PostMapping(value = "/save")
    public BaseResult<Boolean> save(@Valid @RequestBody Role dto){
        boolean result = roleService.insert(dto);
        return Result.ok(result);
    }

    @ApiOperation(value = "删除角色",notes = "根据角色id删除角色",httpMethod = "DELETE")
    @DeleteMapping(value = "/delete/{roleId}")
    public BaseResult<Boolean> delete(@PathVariable(value = "roleId",required = true) List<Long> roleId){
        return Result.ok(roleService.delete(roleId));
    }




    @ApiOperation(value = "角色赋值菜单",notes = "给某一个角色授权",httpMethod = "POST")
    @PostMapping(value = "/authorMenu")
    public BaseResult<Boolean> authorMenu(@RequestBody RoleAuthorMenuDTO dto){
        roleService.authorMenu(dto.getRoleId(),dto.getMenus());
        return Result.ok();
    }

    @ApiOperation(value = "获取角色权限",notes = "根据角色id获取角色权限",httpMethod = "GET")
    @GetMapping(value = "/author/{roleId}")
    public BaseResult<List<MenuTree>> author(@PathVariable(value = "roleId",required = true) Long roleId){
        return Result.ok(roleService.rolePerssions(roleId));
    }

}
